package com.alan.base.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.BlendMode;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.alan.base.App;
import com.alan.base.R;
import com.alan.base.utils.Logger;
import com.alan.base.utils.Utils;

import java.lang.reflect.Type;

public class BottomLayout extends RadioGroup {

    private OnSelectedListener onSelectedListener;

    public BottomLayout(Context context) {
        super(context);
    }

    public BottomLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setData(int[] imageArray, String[] textArray) {
        this.removeAllViews();
        if (null == imageArray || null == textArray || imageArray.length != textArray.length) {
            return;
        }
        int length = imageArray.length;
        setOrientation(LinearLayout.HORIZONTAL);
        setWeightSum(length);
        for (int i = 0; i < length; i++) {
            RadioButton item = createItem(imageArray[i], textArray[i]);
            addView(item);
            if (i == 0) {
                item.setChecked(true);
            }
        }

        setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(null!=onSelectedListener){
                    int checkedIndex = getCheckedIndex();
                    onSelectedListener.onSelectedListener(checkedIndex);
                }
            }
        });
    }

    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public int getCheckedIndex() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            if (view instanceof RadioButton && ((RadioButton) view).isChecked()) {
                return i;
            }
        }
        return -1;
    }

    @SuppressLint("UseCompatTextViewDrawableApis")
    private RadioButton createItem(int imageRes, String text) {
        ColorStateList colorStateList = ActivityCompat.getColorStateList(getContext(), R.color.selector_bottom_text);

        boolean enable = Utils.getAttrBoolean(getContext(), R.attr.bottom_layout_enable_tint);
        Logger.d("bottom_layout_enable_tint:" + enable);

        RadioButton button = new RadioButton(getContext());
        button.setButtonDrawable(null);
        Drawable drawable = ActivityCompat.getDrawable(getContext(), imageRes);
        if (enable) {
            Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
            wrappedDrawable.setTintList(colorStateList);
            wrappedDrawable.setBounds(0, 0, wrappedDrawable.getMinimumWidth(), wrappedDrawable.getMinimumHeight());
            button.setCompoundDrawables(null, wrappedDrawable, null, null);
        } else {
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            button.setCompoundDrawables(null, drawable, null, null);
        }
        button.setText(text);
        button.setCompoundDrawablePadding(Utils.dip2px(2));

        button.setGravity(Gravity.CENTER);

        button.setTextColor(colorStateList);
        float attrDim = Utils.getAttrDim(getContext(), R.attr.bottom_layout_text_size);
        button.setTextSize(TypedValue.COMPLEX_UNIT_PX, attrDim);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        layoutParams.weight = 1;
        button.setLayoutParams(layoutParams);

        int resourceId = Utils.getAttrResourceId(getContext(), R.attr.bottom_layout_item_background);
        button.setBackgroundResource(resourceId);
        int i = Utils.dip2px(8);
        button.setPadding(0, i, 0, i);
        return button;
    }

    public interface OnSelectedListener{

        void onSelectedListener(int index);
    }

}
